<?php defined('SYSPATH') or die('No direct script access.');

/**
 *Copyright www.stourweb.com
 *Author: nilsir
 *QQ: 2758691158
 *Time: 2017/7/24 10:20
 *Desc: 攻略api
 */
class Controller_Pc_Api_Standard_Article extends Controller_Pc_Api_Base
{
    public function before()
    {
        parent::before();
    }

    /**
     * @desc 获取轮播图
     */
    public function action_get_rolling_ad()
    {
        $adname = Common::remove_xss($this->request_body->content->name);
        $result = array();
        if ($adname)
        {
            $result = Model_Api_Standard_Ad::getad(array('name' => $adname));
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_get_xcx_rolling_ad()
    {
        $result = DB::select()->from('xcx_ads')->where('prefix','=','s_article_index')->where('typeid','=',4)
            ->order_by('displayorder','asc')->execute()->as_array();
        foreach($result as &$v){
            //判断产品类型
            if($v['typeid'] == 1){
                $v['url'] = 'stpage/article/pages/show/show';
                $mod = DB::select('litpic')->from('article');
            }
            if(!empty($mod)){
                $row = $mod ->where('id','=',$v['proid'])->execute()->current();
                //增加图片绝对访问地址
                $v['litpic'] = Model_Api_Standard_System::reorganized_resource_url($row['litpic']);
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //首页列表
    public function action_xcx_index_list()
    {
        $pagesize = 8;
        //页码
        $page = 1;
        //目的地
        $params = array(
            'page'     => $page,
            'pagesize' => $pagesize,
            'destpy'   => null,
            'keyword'  => '',
        );
        $result = Model_Api_Standard_Article::search($params);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }
    /*
     * 攻略搜索列表
     */
    public function action_xcx_search()
    {
        $page = intval($this->request_body->content->page);
        $keyword = $this->request_body->content->keyword;
        $destination = $this->request_body->content->destination;
        $params = array(
            'page'     => $page,
            'pagesize' => 10,
            'destpy'   => $destination,
            'keyword'  => $keyword
        );
        $result = Model_Api_Standard_Article::search($params);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //攻略详情
    public function action_xcx_detail()
    {
        $id = intval($this->request_body->content->id);
        if ($id)
        {
            $result = Model_Api_Standard_Article::detail($id);
            if (empty($result))
            {
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key'], false, '查询攻略失败', '查询攻略失败');
            }
            else
            {
                $dest = DB::select()->from('destinations')->where('id','=',$result['finaldestid'])->execute()->current();;
                $destpy = $dest['pinyin'];
                if(!empty($destpy))
                {
                    $params = array(
                        'page'     => 1,
                        'pagesize' => 4,
                        'destpy'   => $destpy
                    );
                    $recommend_result = Model_Api_Standard_Article::search($params);
                    $result['recommend_list'] = $recommend_result['list'];
                    $result['dest'] = $dest;
                }

                DB::query(Database::UPDATE,"UPDATE `sline_article` SET shownum=IFNULL(shownum,0)+1 WHERE id='$id'")->execute();

                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }
        }
    }

    /**
     * @desc 获取攻略列表
     */
    public function action_xcx_list()
    {
        //条数
        $pagesize = intval($this->request_body->content->pagesize);
        //页码
        $page = intval($this->request_body->content->page);
        //目的地
        $destpy = $this->request_body->content->destpy;
        //关键词
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $params = array(
            'page'     => $page,
            'pagesize' => $pagesize,
            'destpy'   => $destpy,
            'keyword'  => $keyword,
        );
        $result = Model_Api_Standard_Article::search($params);

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    /**
     * @desc 栏目信息
     */
    public function action_channel()
    {
        $result = Model_Api_Standard_Article::channel();

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @desc 获取攻略列表
     */
    public function action_list()
    {
        //条数
        $pagesize = intval($this->request_body->content->pagesize);
        //页码
        $page = intval($this->request_body->content->page);
        //目的地
        $destpy = $this->request_body->content->destpy;
        //关键词
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $params = array(
            'page'     => $page,
            'pagesize' => $pagesize,
            'destpy'   => $destpy,
            'keyword'  => $keyword,
        );
        $result = Model_Api_Standard_Article::search($params);

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @desc 攻略详情
     */
    public function action_detail()
    {
        $id = intval($this->request_body->content->productid);
        if ($id)
        {
            $result = Model_Api_Standard_Article::detail($id);

            if (empty($result))
            {
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key'], false, '查询攻略失败', '查询攻略失败');
            }
            else
            {
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }

        }
    }
}